﻿Public Class frm_CS2
    Public TrimTemp As Integer
    Dim AutoTrimVal As Single
    Dim temp1 As Integer, temp2 As Integer, temp3 As Integer


    Private Sub frm_CS2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Label_Refresh(Me.Text)
        Selected_ID = Me.Name
        Timer1.Enabled = True
        Call Form_Setup()
    End Sub

    Private Sub frm_CS2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
        Timer1.Enabled = False
    End Sub

    Private Sub frm_CS2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        swf_CS2.Movie = Application.StartupPath + "\Restricted\SWF\CS2.swf"
        Call Form_Setup()
    End Sub


    Public Sub Form_Setup()
        Dim i As Integer
        Dim temp As String

        If Get_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.Trim_frm.cmb_O.selectedIndex", CS2_O_Trim(Me.Name) + 64)

        If Get_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.Trim_frm.cmb_G.selectedIndex", CS2_G_Trim(Me.Name) + 128)

        If Get_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.Trim_frm.cmb_DO.selectedIndex", CS2_DO_Trim(Me.Name))

        temp = ""
        For i = 0 To 255
            If i < 255 Then
                temp = temp & CS2OCP_Conversion(i, Me.Name) & ","
            Else
                temp = temp & CS2OCP_Conversion(i, Me.Name)
            End If
        Next
        swf_CS2.SetVariable("_root.CS2OCPLimitString", temp)

        temp = ""
        For i = 0 To 7
            If i < 7 Then
                temp = temp & CS2RCP_Conversion(i, Me.Name) & ","
            Else
                temp = temp & CS2RCP_Conversion(i, Me.Name)
            End If
        Next
        swf_CS2.SetVariable("_root.CS2RCPLimitString", temp)

        If Get_Val("CS2 OCP Limit", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.OCPSetting.text", CS2OCP_Conversion(CS2_OCP(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.CS2OCPLimitSet", CS2_OCP(Me.Name))

        '''''''''''''Commented out 2011/11/22 Modified by matt '''''''''''''''''''

        'If Get_Val("CS2 OCP Flag", Me.Name) = False Then GoTo exit_Form_Setup
        'swf_CS2.SetVariable("_root.Flags_frm.cmb_F10.selectedIndex", CS2OCP_F(0, Me.Name))
        'swf_CS2.SetVariable("_root.Flags_frm.cmb_F11.selectedIndex", CS2OCP_F(1, Me.Name))
        'swf_CS2.SetVariable("_root.Flags_frm.cmb_F12.selectedIndex", CS2OCP_F(2, Me.Name))


        '''''''''''''Commented out 2011/11/22 Modified by matt '''''''''''''''''''

        If Get_Val("CS2 RCP Limit", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.RCPSetting.text", CS2RCP_Conversion(CS2_RCP(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.RCPLimitSet", CS2_RCP(Me.Name))


        If Get_Val("CS2 RCP Flag", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.Flags_frm2.cmb_F20.selectedIndex", CS2RCP_F(0, Me.Name))
        swf_CS2.SetVariable("_root.Flags_frm2.cmb_F21.selectedIndex", CS2RCP_F(1, Me.Name))
        swf_CS2.SetVariable("_root.Flags_frm2.cmb_F22.selectedIndex", CS2RCP_F(2, Me.Name))


        If Get_Val("LLMS", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.LLS_frm.cmb_LLS.selectedIndex", LLMS(Me.Name))

        temp = ""
        For i = 0 To 15
            If i < 15 Then
                temp = temp & LLM_Conversion(i, Me.Name) & ","
            Else
                temp = temp & LLM_Conversion(i, Me.Name)
            End If
        Next

        swf_CS2.SetVariable("_root.LLMString", temp)
        If Get_Val("LLM", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.lbl_LL.text", LLM_Conversion(LLM(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.LLMSet", LLM(Me.Name))
        temp = ""

        For i = 0 To 15
            If i < 15 Then
                temp = temp & LLM_Conversion(i, Me.Name) & ","
            Else
                temp = temp & LLM_Conversion(i, Me.Name)
            End If
        Next

        swf_CS2.SetVariable("_root.DLLMString", temp)
        If Get_Val("DLLM", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.lbl_DLL.text", LLM_Conversion(DLLM(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.DLLMSet", DLLM(Me.Name))
        temp = ""

        For i = 0 To 3
            If i < 3 Then
                temp = temp & LLMH_Conversion(0, i, Me.Name) & ","
            Else
                temp = temp & LLMH_Conversion(0, i, Me.Name)
            End If
        Next

        swf_CS2.SetVariable("_root.LLMHString", temp)
        If Get_Val("LLMH", Me.Name) = False Then GoTo exit_Form_Setup



        

        swf_CS2.SetVariable("_root.lbl_LLH.text", LLMH_Conversion(LLM(Me.Name), LLMH(Me.Name), Me.Name))



        swf_CS2.SetVariable("_root.lbl_DLLH.text", LLMH_Conversion(DLLM(Me.Name), LLMH(Me.Name), Me.Name))




        swf_CS2.SetVariable("_root.lbl_LLHys.text", LLMH_Conversion(0, LLMH(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.LLMHSet", LLMH(Me.Name))

        If Get_Val("SwitchFreq", Me.Name) = False Then GoTo exit_Form_Setup

        temp = ""
        For i = 0 To 3
            If i < 3 Then
                temp = temp & LLD_Conversion(i, Me.Name) & ","
            Else
                temp = temp & LLD_Conversion(i, Me.Name)
            End If
        Next
        swf_CS2.SetVariable("_root.LLMDString", temp)


        If Get_Val("LLD", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.lbl_DLLD.text", "Debounce=" & LLD_Conversion(LLD(Me.Name), Me.Name))
        swf_CS2.SetVariable("_root.LLMDSet", LLD(Me.Name))

        If Get_Val("LLMD", Me.Name) = False Then GoTo exit_Form_Setup
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DA.selected", LLMD(0, Me.Name))
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DB.selected", LLMD(1, Me.Name))
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DC.selected", LLMD(2, Me.Name))
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DD.selected", LLMD(3, Me.Name))
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DSR1.selected", LLMD(4, Me.Name))
        swf_CS2.SetVariable("_root.LL_frm.ckbx_DSR2.selected", LLMD(5, Me.Name))

        


        If Get_Val("Light Load Enable", Me.Name) = False Then GoTo exit_Form_Setup
        If Light_Load_E(Me.Name) = 1 Then
            swf_CS2.SetVariable("_root.LLM_ON._visible", 1)
            swf_CS2.SetVariable("_root.LLM_OFF._visible", 0)
        Else
            swf_CS2.SetVariable("_root.LLM_ON._visible", 0)
            swf_CS2.SetVariable("_root.LLM_OFF._visible", 1)
        End If

        swf_CS2.CallFunction("<invoke name=""fill_labels"" returntype=""xml""><arguments><string>""</string></arguments></invoke>")

exit_Form_Setup:

    End Sub

    Private Sub swf_CS2_FSCommand(ByVal sender As Object, ByVal e As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles swf_CS2.FSCommand
        Dim dummy As Boolean

        Select Case e.command
            Case "Close"
                frm_Win_Navi.Del_Win(Me.Text)
                Me.Close()
            Case "Range"
                CS2_Nom(1, Me.Name) = e.args
                dummy = Set_Val("CS2 Nom", Me.Name)
                Call Form_Setup()
            Case "O"
                If Unlock(Me.Name) = True Then
                    CS2_O_Trim(Me.Name) = e.args
                    dummy = Set_Val("CS2 O Trim", Me.Name)
                Else
                    swf_CS2.SetVariable("_root.Trim_frm.cmb_O.selectedIndex", CS2_O_Trim(Me.Name) + 64)
                    swf_CS2.CallFunction("<invoke name=""fill_labels"" returntype=""xml""><arguments><string>""</string></arguments></invoke>")
                End If
            Case "DO"
                If Unlock(Me.Name) = True Then
                    CS2_DO_Trim(Me.Name) = e.args
                    dummy = Set_Val("CS2 DO Trim", Me.Name)
                Else
                    swf_CS2.SetVariable("_root.Trim_frm.cmb_G.selectedIndex", CS2_G_Trim(Me.Name) + 128)
                    swf_CS2.CallFunction("<invoke name=""fill_labels"" returntype=""xml""><arguments><string>""</string></arguments></invoke>")
                End If
            Case "G"
                If Unlock(Me.Name) = True Then
                    CS2_G_Trim(Me.Name) = e.args
                    dummy = Set_Val("CS2 G Trim", Me.Name)
                Else
                    swf_CS2.SetVariable("_root.Trim_frm.cmb_DO.selectedIndex", CS2_DO_Trim(Me.Name))
                    swf_CS2.CallFunction("<invoke name=""fill_labels"" returntype=""xml""><arguments><string>""</string></arguments></invoke>")
                End If
            Case "CS2 OCP"
                CS2_OCP(Me.Name) = e.args
                dummy = Set_Val("CS2 OCP Limit", Me.Name)
            Case "CS2 RCP"
                CS2_RCP(Me.Name) = e.args
                dummy = Set_Val("CS2 RCP Limit", Me.Name)

            
            Case "F10"
                CS2OCP_F(0, Me.Name) = e.args
                dummy = Set_Val("CS2 OCP Flag", Me.Name)
            Case "F11"
                CS2OCP_F(1, Me.Name) = e.args
                dummy = Set_Val("CS2 OCP Flag", Me.Name)
            Case "F12"
                CS2OCP_F(2, Me.Name) = e.args
                dummy = Set_Val("CS2 OCP Flag", Me.Name)
            Case "F20"
                CS2RCP_F(0, Me.Name) = e.args
                dummy = Set_Val("CS2 RCP Flag", Me.Name)
            Case "F21"
                CS2RCP_F(1, Me.Name) = e.args
                dummy = Set_Val("CS2 RCP Flag", Me.Name)
            Case "F22"
                CS2RCP_F(2, Me.Name) = e.args
                dummy = Set_Val("CS2 RCP Flag", Me.Name)
            Case "CCM Window"
                Open_Window("CCM", Global_ID)
            Case "AutoTrim1"
                If Unlock(Me.Name) = True Then
                    swf_CS2.SetVariable("_root.progress_ind.lbl_PI.text", "Please Wait")
                    Timer2.Enabled = True
                Else
                    swf_CS2.SetVariable("_root.progress_ind.lbl_PI.text", "Unlock Trim and Press Auto-Trim Again")
                End If
            Case "AutoTrim2"
                swf_CS2.SetVariable("_root.progress_ind.lbl_PI.text", "Auto Trim in Progress")
                Timer3.Enabled = True
                AutoTrimVal = Val(e.args)
        End Select
    End Sub

    Public Sub AutoTrim1()

        Dim temp_val As Integer
        If Get_Val("CS2 Nom", Me.Name) = False Then GoTo exit_timer
        temp_val = IIf(CS2_Nom(1, Me.Name) = 0, 208, 104)


        'If Get_Val("ORFET A Thresh", Me.Name) = False Then GoTo exit_timer
        'TrimTemp = ORFET_A_Thresh(Me.Name)
        'ORFET_A_Thresh(Me.Name) = 0
        'If Set_Val("ORFET A Thresh", Me.Name) = False Then GoTo exit_timer

        If Get_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
        temp1 = CS2_O_Trim(Me.Name)
        If Get_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer
        temp2 = CS2_DO_Trim(Me.Name)
        If Get_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_timer
        temp3 = CS2_G_Trim(Me.Name)

        CS2_O_Trim(Me.Name) = 0
        If Set_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
        CS2_DO_Trim(Me.Name) = 0
        If Set_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer
        CS2_G_Trim(Me.Name) = 0
        If Set_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_timer

        Call Delay(50)
        If Get_Val("CS2 Reg", Me.Name) = False Then GoTo exit_timer

        If CS2_Reg(Me.Name) > temp_val Then
            CS2_O_Trim(Me.Name) = 64
            While CS2_Reg(Me.Name) > temp_val And CS2_O_Trim(Me.Name) < 128
                If Set_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
                CS2_O_Trim(Me.Name) = CS2_O_Trim(Me.Name) + 1
                Call Delay(50)
                If Get_Val("CS2 Reg", Me.Name) = False Then GoTo exit_timer
            End While
            CS2_O_Trim(Me.Name) = CS2_O_Trim(Me.Name) - 1
            If Set_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
        Else
            CS2_O_Trim(Me.Name) = 0
            While CS2_Reg(Me.Name) < temp_val And CS2_O_Trim(Me.Name) < 64

                If Set_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
                CS2_O_Trim(Me.Name) = CS2_O_Trim(Me.Name) + 1
                Call Delay(50)
                If Get_Val("CS2 Reg", Me.Name) = False Then GoTo exit_timer

            End While
        End If

        If Get_Val("CS2 Reg", Me.Name) = False Then GoTo exit_timer

        If CS2_Reg(Me.Name) < 255 Then

            'CS2_DO_Trim(Me.Name) = CS2_Reg(Me.Name)
            'If Set_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer

            'Call Delay(50)
            'If Get_Val("Flags2", Me.Name) = False Then GoTo exit_timer

            'While Flags2(2, Me.Name) = 1 And CS2_DO_Trim(Me.Name) > 1

            '    CS2_DO_Trim(Me.Name) = CS2_DO_Trim(Me.Name) - 2
            '    If Set_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer

            '    Call Delay(50)
            '    If Get_Val("Flags2", Me.Name) = False Then GoTo exit_timer

            'End While

            'CS2_DO_Trim(Me.Name) = IIf(CS2_DO_Trim(Me.Name) > 1, CS2_DO_Trim(Me.Name) - 2, 0)
            CS2_DO_Trim(Me.Name) = 102
            If Set_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer
        End If
exit_timer:
    End Sub

    Public Sub AutoTrim2(ByVal OPIUIP As Single)

        If Get_Val("CS2", Me.Name) = False Then GoTo exit_timer

        If OPI_Val(Me.Name) < OPIUIP Then
            CS2_G_Trim(Me.Name) = 0
            While OPI_Val(Me.Name) < OPIUIP And CS2_G_Trim(Me.Name) < 32

                If Set_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_timer
                CS2_G_Trim(Me.Name) = (CS2_G_Trim(Me.Name) + 1)
                Call Delay(50)
                If Get_Val("CS2", Me.Name) = False Then GoTo exit_timer

            End While
        Else
            CS2_G_Trim(Me.Name) = 31                                        'Set negative trim
            While OPI_Val(Me.Name) > OPIUIP And CS2_G_Trim(Me.Name) < 63

                If Set_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_timer
                CS2_G_Trim(Me.Name) = (CS2_G_Trim(Me.Name) + 1)
                Call Delay(50)
                If Get_Val("CS2", Me.Name) = False Then GoTo exit_timer

            End While
        End If

        'ORFET_A_Thresh(Me.Name) = TrimTemp
        'If Set_Val("ORFET A Thresh", Me.Name) = False Then GoTo exit_timer

        'swf_CS2.SetVariable("_root.Main._alpha", 0)
        'swf_CS2.SetVariable("_root.AutoTrim_frm2._x", 1020)

        

        If (OPI_Val(Me.Name) < (OPIUIP - (OPIUIP * 0.02))) Or (OPI_Val(Me.Name) > (OPIUIP + (OPIUIP * 0.02))) Then
            'swf_CS2.SetVariable("_root.progress_ind.lbl_PI.text", "Auto Trim Failed, Loading Default Value")
            'CS2_O_Trim(Me.Name) = temp1
            'If Set_Val("CS2 O Trim", Me.Name) = False Then GoTo exit_timer
            'CS2_DO_Trim(Me.Name) = temp2
            'If Set_Val("CS2 DO Trim", Me.Name) = False Then GoTo exit_timer
            'CS2_G_Trim(Me.Name) = temp3
            'If Set_Val("CS2 G Trim", Me.Name) = False Then GoTo exit_timer
        Else
            swf_CS2.SetVariable("_root.progress_ind.lbl_PI.text", "Auto Trim Complete")
        End If

        Timer4.Enabled = True

        Call Form_Setup()

exit_timer:
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer2.Enabled = False
        Call AutoTrim1()
    End Sub

    Public Sub Indicators_Refresh()

        swf_CS2.SetVariable("_root.CS2Val.text", Math.Round(OPI_Val(Me.Name), 2) & " A")
        swf_CS2.SetVariable("_root.Trim_frm.CS2Val.text", Math.Round(OPI_Val(Me.Name), 2) & " A")

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If Get_Val("CS2 Val", Me.Name) = False Then GoTo exit_timer
        Call Indicators_Refresh()

exit_timer:
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        Timer3.Enabled = False
        Call AutoTrim2(AutoTrimVal)
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        Timer4.Enabled = False
        swf_CS2.CallFunction("<invoke name=""AutoTrimComplete"" returntype=""xml""><arguments><string>""</string></arguments></invoke>")
    End Sub

    Private Sub swf_CS2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles swf_CS2.Enter

    End Sub
End Class